#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/12/18 16:05
# @Author  : leon yan
# @Site    : 
# @File    : qPlcFlash.py
# @Software: PyCharm

import time
from threading import Lock

from PyQt5.QtCore import QObject
import lib.applets as sApp
import Globalist as gl


class QPlcPolling(QObject):
    def __init__(self):
        super().__init__()
        # init a thread lock
        self.regs_lock = Lock()
        ''''''

    def plc_thread(self):
        while True:
            read_plc_status()
            read_height()
            read_power()
            writeLinkSignal()
            writeOnline()
            writeConveyorSpeed()
            ip2ip()
            # 0.1s before next polling
            time.sleep(0.1)


def read_plc_status():
    M = gl.global_var["Plc_Interface"]["M600"]
    '''急停'''
    gl.global_var["bEmergencyStopList"][0] = M["0"][0]
    gl.global_var["bEmergencyStopList"][1] = M["1"][1]
    # gl.global_var["bEmergencyStopList"][2] = 0        # 无
    gl.global_var["bEmergencyStopList"][3] = M["1"][0]
    gl.global_var["bEmergencyStopList"][4] = M["2"][0]
    gl.global_var["bEmergencyStopList"][5] = M["3"][0]
    # gl.global_var["bEmergencyStopList"][6] = 0        # 无
    gl.global_var["bEmergencyStopList"][7] = M["4"][0]
    gl.global_var["bEmergencyStopList"][8] = M["5"][0]
    gl.global_var["bEmergencyStopList"][9] = M["6"][0]
    gl.global_var["bEmergencyStopList"][10] = M["6"][1]
    gl.global_var["bEmergencyStopList"][11] = M["7"][0]
    gl.global_var["bEmergencyStopList"][12] = M["8"][0]
    gl.global_var["bEmergencyStopList"][13] = M["8"][1]
    # gl.global_var["bEmergencyStopList"][14] = 0 # 砂光机
    gl.global_var["bEmergencyStopList"][15] = M["11"][1]
    gl.global_var["bEmergencyStopList"][16] = M["10"][0]
    gl.global_var["bEmergencyStopList"][17] = M["11"][0]
    gl.global_var["bEmergencyStopList"][18] = M["12"][0]
    gl.global_var["bEmergencyStopList"][19] = M["13"][0]
    gl.global_var["bEmergencyStopList"][20] = M["13"][1]
    # gl.global_var["bEmergencyStopList"][21] = 0 # 砂光机
    gl.global_var["bEmergencyStopList"][22] = M["15"][0]
    gl.global_var["bEmergencyStopList"][23] = M["15"][1]
    gl.global_var["bEmergencyStopList"][24] = M["16"][0]

    '''报警'''
    gl.global_var["bAlarmList"][0] = M["0"][3]
    gl.global_var["bAlarmList"][1] = M["1"][4]
    # gl.global_var["bAlarmList"][2] = 0 # 无
    gl.global_var["bAlarmList"][3] = M["1"][3]
    gl.global_var["bAlarmList"][4] = M["2"][3]
    gl.global_var["bAlarmList"][5] = M["3"][3]
    gl.global_var["bAlarmList"][6] = M["2"][4]
    gl.global_var["bAlarmList"][7] = M["4"][3]
    gl.global_var["bAlarmList"][8] = M["5"][3]
    gl.global_var["bAlarmList"][9] = M["6"][3]
    gl.global_var["bAlarmList"][10] = M["6"][4]
    gl.global_var["bAlarmList"][11] = M["7"][3]
    gl.global_var["bAlarmList"][12] = M["8"][3]
    gl.global_var["bAlarmList"][13] = M["8"][4]
    # gl.global_var["bAlarmList"][14] = 0 # 砂光机
    gl.global_var["bAlarmList"][15] = M["11"][4]
    gl.global_var["bAlarmList"][16] = M["10"][3]
    gl.global_var["bAlarmList"][17] = M["11"][3]
    gl.global_var["bAlarmList"][18] = M["12"][3]
    gl.global_var["bAlarmList"][19] = M["13"][3]
    gl.global_var["bAlarmList"][20] = M["13"][4]
    # gl.global_var["bAlarmList"][21] = M["1"][0] # 砂光机
    gl.global_var["bAlarmList"][22] = M["15"][3]
    gl.global_var["bAlarmList"][23] = M["15"][4]
    gl.global_var["bAlarmList"][24] = M["16"][3]

    '''运行'''
    gl.global_var["bRunningList"][0] = M["0"][5]
    gl.global_var["bRunningList"][1] = M["1"][6]
    # gl.global_var["bRunningList"][2] = M["1"][0] # 无
    gl.global_var["bRunningList"][3] = M["1"][5]
    gl.global_var["bRunningList"][4] = M["2"][5]
    gl.global_var["bRunningList"][5] = M["3"][5]
    gl.global_var["bRunningList"][6] = M["2"][6]
    gl.global_var["bRunningList"][7] = M["4"][5]
    gl.global_var["bRunningList"][8] = M["5"][5]
    gl.global_var["bRunningList"][9] = M["6"][5]
    gl.global_var["bRunningList"][10] = M["6"][6]
    gl.global_var["bRunningList"][11] = M["7"][5]
    gl.global_var["bRunningList"][12] = M["8"][5]
    gl.global_var["bRunningList"][13] = M["8"][6]
    # gl.global_var["bRunningList"][14] = M["1"][0] # 砂光机
    gl.global_var["bRunningList"][15] = M["11"][6]
    gl.global_var["bRunningList"][16] = M["10"][5]
    gl.global_var["bRunningList"][17] = M["11"][5]
    gl.global_var["bRunningList"][18] = M["12"][5]
    gl.global_var["bRunningList"][19] = M["13"][5]
    gl.global_var["bRunningList"][20] = M["13"][6]
    # gl.global_var["bRunningList"][21] = M["1"][0] # 砂光机
    gl.global_var["bRunningList"][22] = M["15"][5]
    gl.global_var["bRunningList"][23] = M["15"][6]
    gl.global_var["bRunningList"][24] = M["16"][5]


def read_height():
    eHeightDict = gl.global_var["Plc_Interface"]["D2010"]
    aa = gl.global_var["Plc_Interface"]["4X0"]["9"]
    if aa:
        eSander1Height = float(aa[2]) / 100.0
    else:
        eSander1Height = 0.0
    aa2 = gl.global_var["Plc_Interface"]["4X0"]["14"]
    if aa2:
        eSander2Height = float(aa2[2]) / 100.0
    else:
        eSander2Height = 0.0

    gl.global_var["curHeight"]["1"] = [float(u2s(eHeightDict["1"][0])) / 10.0]
    gl.global_var["curHeight"]["3"] = [float(u2s(eHeightDict["3"][0])) / 10.0, float(u2s(eHeightDict["3"][1])) / 10.0]
    gl.global_var["curHeight"]["4"] = [float(u2s(eHeightDict["4"][0])) / 10.0, float(u2s(eHeightDict["4"][1])) / 10.0]
    gl.global_var["curHeight"]["7"] = [float(u2s(eHeightDict["7"][0])) / 10.0, float(u2s(eHeightDict["7"][1])) / 10.0,
                                       float(u2s(eHeightDict["7"][2])) / 10.0]
    gl.global_var["curHeight"]["9"] = [eSander1Height]
    gl.global_var["curHeight"]["10"] = [float(u2s(eHeightDict["10"][0])) / 10.0, float(u2s(eHeightDict["10"][1])),
                                        float(u2s(eHeightDict["10"][2])) / 10.0]
    gl.global_var["curHeight"]["12"] = [float(u2s(eHeightDict["12"][0])) / 10.0,
                                        float(u2s(eHeightDict["12"][1])) / 10.0]
    gl.global_var["curHeight"]["14"] = [eSander2Height]
    gl.global_var["curHeight"]["15"] = [float(u2s(eHeightDict["15"][0])) / 10.0]

    eSetHeightDict = gl.global_var["Plc_Interface"]["D3000"]
    gl.global_var["setHeight"]["1"] = [float(u2s(eSetHeightDict["1"][0])) / 10.0]
    gl.global_var["setHeight"]["3"] = [float(u2s(eSetHeightDict["3"][0])) / 10.0,
                                       float(u2s(eSetHeightDict["3"][1])) / 10.0]
    gl.global_var["setHeight"]["4"] = [float(u2s(eSetHeightDict["4"][0])) / 10.0,
                                       float(u2s(eSetHeightDict["4"][1])) / 10.0]
    gl.global_var["setHeight"]["7"] = [float(u2s(eSetHeightDict["7"][0])) / 10.0,
                                       float(u2s(eSetHeightDict["7"][1])) / 10.0,
                                       float(u2s(eSetHeightDict["7"][2])) / 10.0]
    gl.global_var["setHeight"]["9"] = [eSander1Height]
    gl.global_var["setHeight"]["10"] = [float(u2s(eSetHeightDict["10"][0])) / 10.0, float(u2s(eSetHeightDict["10"][1])),
                                        float(u2s(eSetHeightDict["10"][2])) / 10.0]
    gl.global_var["setHeight"]["12"] = [float(u2s(eSetHeightDict["12"][0])) / 10.0,
                                        float(u2s(eSetHeightDict["12"][1])) / 10.0]
    gl.global_var["setHeight"]["14"] = [eSander2Height]
    gl.global_var["setHeight"]["15"] = [float(u2s(eSetHeightDict["15"][0])) / 10.0]


def read_power():
    aa = gl.global_var["watt_hour_meter_ip"]
    aa2 = 0.0
    if aa:
        for k, v in aa.items():
            aa2 += v[2]
    gl.global_var["monitorVar"]["TotalPower"] = round(aa2, 2)


def u2s(sData):
    if sData > 32767:
        return sData - 65536
    else:
        return sData


'''连锁信号'''


def writeLinkSignal():
    M = gl.global_var["Plc_Interface"]["M600"]
    gl.global_var["Plc_write"]["link_signal"]["0"][1] = [M["1"][6]]
    gl.global_var["Plc_write"]["link_signal"]["1"][1] = [M["2"][5]]
    gl.global_var["Plc_write"]["link_signal"]["2"][1] = [M["3"][5], M["4"][5]]
    gl.global_var["Plc_write"]["link_signal"]["3"][1] = [M["2"][6]]
    gl.global_var["Plc_write"]["link_signal"]["4"][1] = [M["5"][5]]
    gl.global_var["Plc_write"]["link_signal"]["5"][1] = [M["6"][5]]
    gl.global_var["Plc_write"]["link_signal"]["6"][1] = [M["6"][6], M["7"][5]]
    gl.global_var["Plc_write"]["link_signal"]["7"][1] = [M["8"][5]]
    gl.global_var["Plc_write"]["link_signal"]["8"][1] = [M["8"][6], 1]
    # gl.global_var["link_signal"]["9"][1] = 1
    gl.global_var["Plc_write"]["link_signal"]["10"][1] = [M["11"][5]]
    gl.global_var["Plc_write"]["link_signal"]["11"][1] = [M["12"][5], M["10"][5]]
    gl.global_var["Plc_write"]["link_signal"]["12"][1] = [M["13"][5]]
    gl.global_var["Plc_write"]["link_signal"]["13"][1] = [M["13"][6], 1]
    # gl.global_var["link_signal"]["14"][1] = 1
    gl.global_var["Plc_write"]["link_signal"]["15"][1] = [M["15"][6], M["16"][5]]


# 联机信号

def writeOnline():
    bOnline = gl.global_var["operateVar"]["bLineOn"]
    aa = gl.global_var["Plc_write"]["bOnlineDict"]
    for key, value in aa.items():
        gl.global_var["Plc_write"]["bOnlineDict"][key][1] = [bOnline]


def writeConveyorSpeed():
    eConveyor = int(gl.global_var["operateVar"]["lineVelocitySet"] * 10)
    for key, value in gl.global_var["Plc_write"]["SpeedSet"].items():
        gl.global_var["Plc_write"]["SpeedSet"][key][1] = [eConveyor]


# ip合并 --更新通讯状态
def ip2ip():
    for key, value in gl.global_var["deviceDict"].items():
        ip = value[2]
        for sK, sV in gl.global_var["deviceIpDict"].items():
            if sV[0] == ip:
                gl.global_var["deviceDict"][key][3] = sV[1]
